class Solution {
public:
    vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
        sort(people.begin(), people.end(), [](vector<int> &a, vector<int> &b) {
            if (a[0] != b[0]) return a[0] > b[0];
            return a[1] < b[1];
        });
        list<vector<int>> queue;
        for (int i = 0; i < people.size(); i++) {
            int pos = people[i][1];
            auto it = queue.begin();
            advance(it, pos);
            queue.insert(it, people[i]);
        } 
        vector<vector<int>> result(queue.begin(), queue.end());
        return result;
    }
};
